package com.hljx.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hljx.dto.AccountingDataPageDTO;
import com.hljx.dto.ExportAccountingUnitDTO;
import com.hljx.excel.ExcelTransfer;
import com.hljx.vo.AccountingUnitDataVO;
import com.hljx.vo.AccountingUnitsVO;
import com.hljx.config.exception.DataException;
import com.hljx.dto.AccountingUnitDTO;
import com.hljx.dto.AccountingUnitPageDTO;
import com.hljx.entity.SysAccountingUnit;
import com.hljx.mapper.sys.SysAccountingUnitMapper;
import com.hljx.service.SysAccountingUnitService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.hljx.vo.TargetSysDictVO;
import jakarta.servlet.http.HttpServletResponse;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 核算单元管理 服务实现类
 * </p>
 *
 * @author gx
 * @since 2023-09-18
 */
@Service
public class SysAccountingUnitServiceImpl extends ServiceImpl<SysAccountingUnitMapper, SysAccountingUnit> implements SysAccountingUnitService {

    @Autowired
    private ExcelTransfer<SysAccountingUnit> excelTransfer;

    @Autowired
    private SysAccountingUnitMapper sysAccountingUnitMapper;

    @Override
    public List<SysAccountingUnit> listAllAccountingUnit() {
        List<SysAccountingUnit> sysAccountingUnitList =
                baseMapper.selectList(new LambdaQueryWrapper<SysAccountingUnit>());
        List<SysAccountingUnit> list = new ArrayList<>();
        for (SysAccountingUnit sysAccountingUnit : sysAccountingUnitList) {
            SysAccountingUnit accountingUnitVO = new SysAccountingUnit();
            BeanUtil.copyProperties(sysAccountingUnit, accountingUnitVO);
            list.add(accountingUnitVO);
        }
        return list;
    }

    @Override
    public Boolean saveOrUpdate(AccountingUnitDTO dto) {
        long count = count(Wrappers.lambdaQuery(SysAccountingUnit.class)
                .eq(SysAccountingUnit::getCode, dto.getCode())
                .ne(dto.getId() != null, SysAccountingUnit::getId, dto.getId()));
        if (count > 0) {
            throw new DataException("核算单元编码重复",201);
        }
        SysAccountingUnit sysAccountingUnit = new SysAccountingUnit();
        BeanUtil.copyProperties(dto, sysAccountingUnit);
        return saveOrUpdate(sysAccountingUnit);
    }

    @Override
    public IPage<AccountingUnitsVO> listAccountingUnitByPage(AccountingUnitPageDTO dto) {
        Page<AccountingUnitsVO> page = new Page<>(dto.getPageNum(), dto.getPageSize());
        return baseMapper.listAccountingUnitByPage(page, dto);
    }

    @Override
    public Boolean removeAccountingById(Long id) {
        return removeById(id);
    }

    @Override
    public List<AccountingUnitDataVO> listAccountingDataByPage(AccountingDataPageDTO dto) {
        QueryWrapper<SysAccountingUnit> queryWrapper = new QueryWrapper<>();
        if (StringUtils.isNotBlank(dto.getName())) {
            queryWrapper.lambda().like(SysAccountingUnit::getName, dto.getName());
        }
        queryWrapper.lambda().eq(SysAccountingUnit::getDeleteFlag, 0);
        return baseMapper.listAccountingDataByPage(queryWrapper);
    }

    @Override
    public void export(ExportAccountingUnitDTO dto, HttpServletResponse response) throws ClassNotFoundException {
        if (dto.getType() == null) {
            throw new DataException("入参数据为空",201);
        }
        List<SysAccountingUnit> list = new ArrayList<>();
        if (dto.getType() == 1) {
            list = baseMapper.listAccountingVO(dto);
        }
        excelTransfer.exportExcel(response, list, "核算单元", "sheet1", SysAccountingUnit.class);
    }

    @Override
    public void importExcel(MultipartFile file, HttpServletResponse response) throws ClassNotFoundException, IOException {
        List<SysAccountingUnit> list = list(Wrappers.lambdaQuery(SysAccountingUnit.class));
        boolean b = excelTransfer.importExcel(file, this, lists -> {
            ArrayList<SysAccountingUnit> arrayList = new ArrayList<>();
            lists.forEach(e -> {
                if (list.stream().noneMatch(l -> e.getName().equals(l.getName()))) {
                    arrayList.add(e);
                }
            });
            return arrayList;
        });
    }

    @Override
    public List<TargetSysDictVO> sysAccountingUnitPull() {
        List<TargetSysDictVO> targetSysDictVOS = sysAccountingUnitMapper.sysAccountingUnitPull();
        return targetSysDictVOS;
    }

    @Override
    public List<TargetSysDictVO> HISDeptCode() {
        List<TargetSysDictVO> targetSysDictVOS = sysAccountingUnitMapper.HISDeptCode();
        return targetSysDictVOS;
    }
}
